<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Storefront_View_Helper
 * @subpackage CategoryTree
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The helper for categories
 * 
 * @category   Zend
 * @package    Storefront_View_Helper
 * @subpackage CategoryTree
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Storefront_View_Helper_CategoryTree extends Zend_View_Helper_Abstract
{

    /**
     * Get HTML for the category tree
     */
    public function categoryTree()
    {
        $output = "";
        $language = $this->view->language;
        $categoryTree = $this->view->categoryTree;
        //check children of root
        if ($categoryTree && $categoryTree->children) {
            $output .= '<ul id="tree" class="treeview">';
            for ($i = 0; $i < count($categoryTree->children); $i++) {
                $category = $categoryTree->children[$i];
                if ($i == count($categoryTree->children) - 1) {
                    $output .= '<li class="last">';
                } else {
                    $output .= '<li>';
                }
                $output .= '<a href="/' . $language . '/catalog/' . $category->id . '">';
                $output .= $this->view->escape($category->name) . '</a>';

                //check children of category
                if ($category->children) {
                    $output .= '<ul>';
                    for ($j = 0; $j < count($category->children); $j++) {
                        $subCategory = $category->children[$j];
                        if ($j == count($category->children) - 1) {
                            $output .= '<li class="last">';
                        } else {
                            $output .= '<li>';
                        }
                        $output .= '<a href="/' . $language . '/catalog/' . $subCategory->id . '">';
                        $output .= $this->view->escape($subCategory->name) . '</a>';

                        // check children of subcategory
                        if ($subCategory->children) {
                            $output .= '<ul>';
                            for ($k = 0; $k < count($subCategory->children); $k++) {
                                $subSubCategory = $subCategory->children[$k];
                                if ($k == count($subCategory->children) - 1) {
                                    $output .= '<li class="last">';
                                } else {
                                    $output .= '<li>';
                                }
                                $output .= '<a href="/' . $language . '/catalog/' . $subSubCategory->id . '">';
                                $output .= $this->view->escape($subSubCategory->name) . '</a>';
                                $output .= '</li>';
                            }
                            $output .= '</ul>';
                        }
                        $output .= '</li>';
                    }
                    $output .= '</ul>';
                }
                $output .= '</li>';
            }
            $output .= '</ul>';
        }
        return $output;
    }

}

